package com.charwayh;

import com.charwayh.service.AsyncHelloService;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author charwayH
 */
public class XMLConsumerMain {
    public static void main(String[] args) throws IOException {
        //AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        context.start();
        // 获取消费者组件
        AsyncHelloService service = context.getBean(AsyncHelloService.class);
        while (true) {
            System.in.read();
            try {
            String result = service.asyncSayHello("haha", 1000);
            // 利用Future模式来获取
            Future<Object> future = RpcContext.getContext().getFuture();
            System.out.println("result: " + result);
            System.out.println("future result: " + future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}